#!/bin/bash

# shellcheck source=/dev/null
source "${JAliasSrc:?}/scripts/tool.bash"
source "${JAliasSrc:?}/scripts/core.sh"

_jalias_completions() {
    files=()
    IFS=" " read "${read_arg:?}" files <<<"$(_jalias_keys)"
    cur="${COMP_WORDS[COMP_CWORD]}"
    prefix_path="${cur%/*}"

    real_path=$(_jalias_replace "$prefix_path")
    if [ -e "$real_path" ]; then
        for file in "$real_path"/*; do
            name=$(basename "$file")
            if [ -d "$file" ]; then
                files+=("$prefix_path/$name/")
            else
                files+=("$prefix_path/$name")
            fi
        done
    fi

    COMPREPLY=($(compgen -W "${files[*]}" -- "$cur"))
}

complete -F _jalias_completions -o nospace jalias
